﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using DB.SqlHelper;
using Framework.Common.Common;
using Mode;

namespace Framework.DataAccess
{
    public class GenerateTopic
    {
        private int role = -1;
        private int publicrole = -1;
        private int candidateid;
        private model_table mt = new model_table();
        private examination_table et = new examination_table();

        private DbInstance db = DbManager.GetDbInstance();
        private ExamDataManage edm = new ExamDataManage();

        public GenerateTopic(int examID, int candidateID)
        {
            candidateid = candidateID;

            get_public_role();
            get_model_by_exam(examID);
            get_role_by_exam();
        }

        public ExamDataManage Generate()
        {
            get_comput_by_model();

            generate_public();

            save_topic(true);

            return edm;
        }

        
        public ExamDataManage GenerateView()
        {
            //get_comput_by_sheet();

            generate_public();

            save_topic(false);

            return edm;
        }
         


        // 取得公共角色的ID
        private void get_public_role()
        {
            publicrole = db.ExecuteScalarBySql("select id from role_table where [power] = 2").ToInt();
        }

        // 取得考场信息与模型信息
        private void get_model_by_exam(int examid)
        {
            string sql = "select  ";
            sql += "examination_table.id as E_ID, ";
            sql += "examination_table.name as E_NAME, ";
            sql += "examination_table.model as E_MODEL, ";
            sql += "examination_table.[date] as E_DATE, ";
            sql += "examination_table.[begin] as E_BEGIN, ";
            sql += "examination_table.[end] as E_END, ";
            sql += "examination_table.[time] as E_TIME, ";
            sql += "examination_table.[pass] as E_PASS, ";
            sql += "examination_table.[status] as E_STATUS, ";
            sql += "examination_table.[has_QA] as E_QA, ";
            sql += "model_table.[id] as M_ID, ";
            sql += "model_table.[name] as M_NAME, ";
            sql += "model_table.[type] as M_TYPE, ";
            sql += "model_table.[date] as M_DATE, ";
            sql += "model_table.[status] as M_STATUS, ";
            sql += "model_table.[model_score] as M_SCORE ";
            sql += "from examination_table  ";
            sql += "join model_table on model_table.id = examination_table.model ";
            sql += "where examination_table.id = " + examid;

            DataTable dt = db.ExecuteDataTableBySql(sql);
            if (dt.Rows.Count != 1)
                throw new Exception("获取考场模型异常 [exam = " + examid + " ]");

            DataRow row = dt.Rows[0];

            // 取得考场信息
            et.begin = Convert.ToDateTime(row["E_BEGIN"]);
            et.date = Convert.ToDateTime(row["E_DATE"]);
            et.end = Convert.ToDateTime(row["E_END"]);
            et.has_QA = row["E_QA"].ToBool();
            et.id = examid;
            et.model = row["E_MODEL"].ToInt();
            et.name = row["E_NAME"].ToStr();
            et.pass = row["E_PASS"].ToInt();
            et.status = row["E_STATUS"].ToInt();
            et.time = row["E_TIME"].ToInt();

            // 取得模型信息
            mt.date = Convert.ToDateTime(row["M_DATE"]);
            mt.has_QA = et.has_QA;
            mt.id = row["M_ID"].ToInt();
            mt.model_score = row["M_SCORE"].ToInt();
            mt.name = row["M_NAME"].ToStr();
            mt.status = row["M_STATUS"].ToInt();
            mt.type = row["M_TYPE"].ToInt();

        }

        // 取得模型对应角色数量
        private void get_role_by_exam()
        {
            string sql = "select  ";
            sql += "are_role_join.role  ";
            sql += "from model_are_role ";
            sql += "join are_role_join on are_role_join.id = model_are_role.areRoleID ";
            sql += "where are_role_join.[status] != 0  ";
            sql += "and model_are_role.modelID =" + mt.id;
            sql += " group by are_role_join.role ";

            DataTable dt = db.ExecuteDataTableBySql(sql);

            if (dt.Rows.Count == 1) role = dt.Rows[0]["role"].ToInt();
            else if (dt.Rows.Count < 1) throw new Exception(" 获取模型对应角色异常[ model id = " + mt.id + " ] ");

        }

        //  取得模型的计算信息- 并取得题目
        private void get_comput_by_model()
        {
            if (mt.type == 1)
                get_topic_by_compute();
            else
            {
                string sql = "select * from compute_table where model = " + mt.id;
                IDataReader reader = db.ExecuteReaderBySql(sql);
                IList<compute_table> ctp = DbManager.FillList<compute_table>(reader);
                if (!reader.IsClosed) reader.Close();

                foreach (compute_table item in ctp)
                    get_topic_by_random(item);
            }
        }

        ////  取得考过的题目
        //private void get_comput_by_sheet()
        //{
 
        //}

        // 根据计算类型取题目-随机
        private void get_topic_by_random(compute_table item)
        {
            StringBuilder sb = new StringBuilder();

            string tmp = "select TOP {0} * from topic_view ";
            tmp += "where topic_view.T_STATUS = 0 ";
            tmp += "and topic_view.T_TYPE = {1} ";
            tmp += "and topic_view.R_ID = {2} ";
            tmp += "order by NEWID(); ";

            // 取角色题
            if (role != -1)
                sb.Append(String.Format(tmp, item.rolecount, item.type, role));

            // 取公共题
            sb.Append(String.Format(tmp, item.publiccount, item.type, publicrole));

            StringBuilder topicidString = new StringBuilder();
            DataSet ds = db.ExecuteDataSetBySql(sb.ToString());
            
            foreach (DataTable dt in ds.Tables)
                foreach (DataRow row in dt.Rows)
                    topicidString.Append(row["T_ID"].ToInt() + ",");

            string sql = "select * from answer_table where topic in ({0})";
            sql = String.Format(sql, topicidString.ToString().TrimEnd(','));
            DataView answer_dt = db.ExecuteDataTableBySql(sql).DefaultView;

            foreach( DataTable dt in ds.Tables )
                foreach (DataRow row in dt.Rows)
                {
                    ExmTopic extm = new ExmTopic();

                    extm.id = row["T_ID"].ToInt();
                    extm.score = item.score;
                    extm.title = row["T_TITLE"].ToString().Replace("\r", "<br>").Replace("\n", "<br>");
                    extm.type = item.type;
                    answer_dt.RowFilter = String.Format("topic={0}",extm.id);
                    extm.answer = new List<TopicAnswer>();
                    foreach (DataRowView drv in answer_dt)
                    {
                        TopicAnswer ta = new TopicAnswer();
                        ta.id = drv["id"].ToInt();
                        ta.answer = drv["answer"].ToString().Replace("\r", "<br>").Replace("\n", "<br>");
                        ta.right = drv["right"].ToInt();
                        ta.code = drv["code"].ToInt();
                        ta.date = Convert.ToDateTime(drv["date"]);
                        ta.topic = extm.id;

                        if (extm.answer == null) extm.answer = new List<TopicAnswer>();
                        extm.answer.Add(ta);
                    }
                    AppendExamTopic(extm);
                }

            answer_dt.Dispose();
            ds.Dispose();
        }

        // 取得手工题,题目-手工
        private void get_topic_by_compute()
        {
            string sql = "Select A.*,B.score From topic_table As A Inner Join compute_table As B On(A.id=B.topic) Where A.status=0 And B.model=" + mt.id;

            StringBuilder sb = new StringBuilder();
            DataTable dt = db.ExecuteDataTableBySql(sql);
            foreach (DataRow row in dt.Rows)
                sb.Append(row["id"].ToInt()+",");

            DataView answer_view = db.ExecuteDataTableBySql("select * from answer_table where topic in (" + sb.ToString().TrimEnd(',') + ")").DefaultView;

            foreach (DataRow row in dt.Rows)
            {
                ExmTopic extm = new ExmTopic();
                extm.id = row["id"].ToInt();
                extm.score = row["score"].ToInt();
                extm.title = row["title"].ToString().Replace("\r", "<br>").Replace("\n", "<br>");
                extm.type = row["type"].ToInt();

                answer_view.RowFilter = String.Format("topic={0}", extm.id);
                foreach (DataRowView drv in answer_view)
                {
                    TopicAnswer ta = new TopicAnswer();
                    ta.id = drv["id"].ToInt();
                    ta.answer = drv["answer"].ToString().Replace("\r", "<br>").Replace("\n", "<br>");
                    ta.right = drv["right"].ToInt();
                    ta.code = drv["code"].ToInt();
                    ta.date = Convert.ToDateTime(drv["date"]);
                    ta.topic = extm.id;

                    if (extm.answer == null) extm.answer = new List<TopicAnswer>();
                    extm.answer.Add(ta);
                }
                AppendExamTopic(extm);
            }
            dt.Dispose();
        }

        // 添加题目
        private void AppendExamTopic(ExmTopic value)
        {
            switch (value.type)
            {
                case 1:
                    if (edm.topic1 == null)
                        edm.topic1 = new List<ExmTopic>();
                    edm.topic1.Add(value);
                    break;
                case 2:
                    if (edm.topic2 == null)
                        edm.topic2 = new List<ExmTopic>();
                    edm.topic2.Add(value);
                    break;
                case 3:
                    if (edm.topic3 == null)
                        edm.topic3 = new List<ExmTopic>();
                    edm.topic3.Add(value);
                    break;
                case 4:
                    if (edm.topic4 == null)
                        edm.topic4 = new List<ExmTopic>();
                    edm.topic4.Add(value);
                    break;
                case 5:
                    if (edm.topic5 == null)
                        edm.topic5 = new List<ExmTopic>();
                    edm.topic5.Add(value);
                    break;
                case 6:
                    if (edm.topic6 == null)
                        edm.topic6 = new List<ExmTopic>();
                    edm.topic6.Add(value);
                    break;
            }
        }

        // 填充公共信息
        private void generate_public()
        {
            edm.comminfo = new ExamCommInfo();

            edm.comminfo.examid = et.id;
            edm.comminfo.examname = et.name;
            edm.comminfo.modeid = mt.id;
            edm.comminfo.modename = mt.name;
            edm.comminfo.totaltime = et.time;
            edm.comminfo.totalH = et.time / 60;
            edm.comminfo.totalM = et.time % 60;
        }

        // 统计题目信息,并保存
        private void save_topic(bool has_save )
        {
            string sql = "insert sheet_table( candidate, topic, topictype, score, [full] ) values( {0}, {1}, {2}, 0, {3});\r\n";
            StringBuilder sbSql = new StringBuilder();
            DbManager.BeginTransaction();
            DbInstance td = DbManager.GetDbInstance();
            try
            {
                // 单选
                if (edm.topic1 == null) edm.topic1 = new List<ExmTopic>();
                foreach (ExmTopic item in edm.topic1)
                {
                    edm.comminfo.totalscore += item.score;
                    edm.comminfo.totaltopic++;

                    edm.topicscore1 += item.score;

                    //if (has_save )
                    //    td.ExecuteNonQueryBySql(String.Format(sql, candidateid, item.id, item.type, item.score));
                    sbSql.AppendFormat(sql, candidateid, item.id, item.type, item.score);
                }
                edm.topicLen1 = edm.topic1.Count;

                //////////////////////////////////////////////////////////////////////////
                // 多选
                if (edm.topic2 == null) edm.topic2 = new List<ExmTopic>();
                foreach (ExmTopic item in edm.topic2)
                {
                    edm.comminfo.totalscore += item.score;
                    edm.comminfo.totaltopic++;

                    edm.topicscore2 += item.score;

                    //if (has_save)
                    //    td.ExecuteNonQueryBySql(String.Format(sql, candidateid, item.id, item.type, item.score));
                    sbSql.AppendFormat(sql, candidateid, item.id, item.type, item.score);
                }
                edm.topicLen2 = edm.topic2.Count;

                ////////////////////////////////////////////////////////////////////////////
                // 判断
                if (edm.topic3 == null) edm.topic3 = new List<ExmTopic>();
                foreach (ExmTopic item in edm.topic3)
                {
                    edm.comminfo.totalscore += item.score;
                    edm.comminfo.totaltopic++;

                    edm.topicscore3 += item.score;

                    //if (has_save)
                    //    td.ExecuteNonQueryBySql(String.Format(sql, candidateid, item.id, item.type, item.score));
                    sbSql.AppendFormat(sql, candidateid, item.id, item.type, item.score);
                }
                edm.topicLen3 = edm.topic3.Count;

                ////////////////////////////////////////////////////////////////////////////
                // 填空
                if (edm.topic4 == null) edm.topic4 = new List<ExmTopic>();
                foreach (ExmTopic item in edm.topic4)
                {
                    edm.comminfo.totalscore += item.score;
                    edm.comminfo.totaltopic++;

                    edm.topicscore4 += item.score;

                    //if (has_save)
                    //    td.ExecuteNonQueryBySql(String.Format(sql, candidateid, item.id, item.type, item.score));
                    sbSql.AppendFormat(sql, candidateid, item.id, item.type, item.score);
                }
                edm.topicLen4 = edm.topic4.Count;

                /////////////////////////////////////////////////////////////////////////////
                // 问答
                if (edm.topic5 == null) edm.topic5 = new List<ExmTopic>();
                foreach (ExmTopic item in edm.topic5)
                {
                    edm.comminfo.totalscore += item.score;
                    edm.comminfo.totaltopic++;

                    edm.topicscore5 += item.score;

                    //if (has_save)
                    //    td.ExecuteNonQueryBySql(String.Format(sql, candidateid, item.id, item.type, item.score));
                    sbSql.AppendFormat(sql, candidateid, item.id, item.type, item.score);
                }
                edm.topicLen5 = edm.topic5.Count;

                /////////////////////////////////////////////////////////////////////////////
                // 打字
                if (edm.topic6 == null) edm.topic6 = new List<ExmTopic>();
                foreach (ExmTopic item in edm.topic6)
                {
                    edm.comminfo.totalscore += item.score;
                    edm.comminfo.totaltopic++;

                    edm.topicscore6 += item.score;

                    //if (has_save)
                    //    td.ExecuteNonQueryBySql(String.Format(sql, candidateid, item.id, item.type, item.score));
                    sbSql.AppendFormat(sql, candidateid, item.id, item.type, item.score);
                }
                edm.topicLen6 = edm.topic6.Count;

                //批量执行插入
                if (has_save)
                {
                    if (sbSql.Length > 0)
                        td.ExecuteNonQueryBySql(sbSql.ToString());
                }
                DbManager.Commit();
            }
            catch (Exception ex)
            {
                DbManager.Rollback();
                throw ex;
            }

        }

    }



    /*
    public class GenerateTopic : model_table
    {
        // 考场的信息
        private examination_table examination = null;

        // 模型附加信息
        private IList<compute_table> compute = null;
        // 模型的区域角色
        private are_table model_are = null;
        private role_table model_role = null;
        // 应答者ID
        private int candidateID = -1;

        ExamDataManage edm = new ExamDataManage();
        private DbInstance db;

        public GenerateTopic(int examID, int candidate)
        {
            db = DbManager.GetDbInstance();

            // 应答者ID
            this.candidateID = candidate;
            // 获取考场信息
            get_examination_by_id(examID);
            // 根据考场取得模型信息
            get_mode_by_exam();
            // 取得考场的角色与区域
            get_arerole();

            // 取得考场的公共信息
            // get_examination_information();
        }

        // 根据考场ID取得考场信息
        private void get_examination_by_id(int examID)
        {
            string sql = "SELECT * FROM examination_table WHERE id=" + examID;
            DataView view = db.ExecuteDataTableBySql(sql).DefaultView;


            if (view.Count != 1)
                throw new Exception("考场信息获取出错.\n[examination_table]:" + examID);

            examination = new examination_table();
            examination.id = view[0]["id"].ToInt();
            examination.name = view[0]["name"].ToString();
            examination.model = view[0]["model"].ToInt();
            examination.date = Convert.ToDateTime(view[0]["date"]);
            examination.begin = Convert.ToDateTime(view[0]["begin"]);
            examination.end = Convert.ToDateTime(view[0]["end"]);
            examination.time = view[0]["time"].ToInt();
            examination.pass = view[0]["pass"].ToInt();
            examination.status = view[0]["status"].ToInt();

            if (examination.status != 0)
                throw new Exception("考场已被禁用,请与管理员联系. [examination_table]:" + examination.id);
        }

        // 根据考场取得模型信息
        private void get_mode_by_exam()
        {
            string sql = "SELECT model_table.* FROM model_table"
                    + " JOIN examination_table ON examination_table.model = model_table.id"
                    + " WHERE examination_table.id =" + examination.id;

            DataView view = db.ExecuteDataTableBySql(sql).DefaultView;
            if (view.Count != 1)
                throw new Exception("考场模型对映出错.\n[examination] : " + examination.id);

            this.id = view[0]["id"].ToInt();
            this.name = view[0]["name"].ToString();
            this.type = view[0]["type"].ToInt();
            this.date = Convert.ToDateTime(view[0]["date"]);
            this.status = view[0]["status"].ToInt();
            //this.are_role = view[0]["are_role"].ToInt();

            if (this.status != 0)
                throw new Exception("模型已被禁用.请与管理员联系\n[model]:" + this.id);

            // 取得模型的附加信息
            sql = "SELECT * FROM compute_table WHERE model = " + this.id;
            IDataReader reader = db.ExecuteReaderBySql(sql);
            compute = DbManager.FillList<compute_table>(reader);
            if (!reader.IsClosed) reader.Close();
            if (compute.Count < 1)
                throw new Exception("模型信息获取失败.\n" + "[model] :" + this.id);
        }

        // 取得考场的公共信息
        private void get_examination_information()
        {

            int[] total = ( this.type ==1 ? TotalManualCount():TotalRandomCount());
            
            edm.comminfo = new ExamCommInfo();

            edm.comminfo.examid = examination.id;
            edm.comminfo.examname = examination.name;
            edm.comminfo.modeid = this.id;
            edm.comminfo.modename = this.name;
            edm.comminfo.totaltopic = total[0];
            edm.comminfo.totalscore = total[1];
            edm.comminfo.totaltime = examination.time;
            edm.comminfo.totalH = examination.time / 60;
            edm.comminfo.totalM = examination.time % 60;
        }

        // 取得考场的区域角色信息
        private void get_arerole()
        {
            //string sql = "SELECT * FROM are_role_view WHERE J_ID=" + this.are_role;
            //DataView view = db.ExecuteDataTableBySql(sql).DefaultView;

            //if (view.Count != 1)
            //    throw new Exception("取得区域角色信息出错.[are_role_join]:" + this.are_role);

            //model_are = new are_table(view[0]["A_ID"].ToInt(), view[0]["A_NAME"].ToString(), view[0]["A_LEVEL"].ToInt());
            //model_role = new role_table();
            //model_role.id = view[0]["R_ID"].ToInt();
            //model_role.name = view[0]["R_NAME"].ToString();
            //model_role.power = new POWER(view[0]["R_POWER"].ToInt());
            //model_role.date = Convert.ToDateTime(view[0]["R_DATE"]);

        }

        //计算本次考场的总题数,与总分数.
        private int[] TotalRandomCount()
        {
            int[] result = new int[2];
            foreach (compute_table item in compute)
            {
                // 本次考试的总题数
                int number = item.publiccount + item.rolecount;
                result[0] += number;
                // 本次考试的分数
                result[1] += number * item.score;
            }
            return result;
        }

        //计算本次考场总题目数,与总分数
        private int[] TotalManualCount()
        {
            int[] result = new int[2];
            DataView view = db.ExecuteDataTableBySql("SELECT  COUNT(TOPIC) as [count] ,SUM(score) AS [score]  FROM compute_table WHERE model = " + this.id).DefaultView;
            result[0] = view[0]["count"].ToInt();
            result[1] = view[0]["score"].ToInt();

            return result;
        }

        //取得公共角色的ID
        private int get_public_roleid()
        {
            return db.ExecuteScalarBySql("select id from role_table where power=2").ToInt();
        }


        // 根据题目类型取得题目计算信息
        private compute_table get_comput_by_type( int type )
        {
            foreach (compute_table item in compute)
                if (item.type == type )
                    return item;
            return null;
        }

        // 生成随机题,根据角色, 类型, 数量取得题目
        private List<ExmTopic> get_randomtopic(int roleID, int type, int count, int score)
        {
            List<ExmTopic> result = new List<ExmTopic>();

            string sql = "SELECT TOP " + count + " * FROM TOPIC_TABLE "
           + " JOIN topic_role_join ON topic_role_join.topic = topic_table.id "
           + " WHERE topic_role_join.role = " + roleID
           + " AND topic_table.status = 0 "
           + " AND topic_table.type = " + type
           + " order by newid()";

            IDataReader reader = db.ExecuteReaderBySql(sql);
            IList<topic_table> list = DbManager.FillList<topic_table>(reader);

            foreach (topic_table item in list)
            {
                ExmTopic et = new ExmTopic();
                et.id = item.id;
                et.title = item.title;
                et.type = type;
                et.score = score;

                string answer_sql = "SELECT * FROM ANSWER_TABLE WHERE TOPIC = " + item.id;
                IDataReader ansread = db.ExecuteReaderBySql(answer_sql);
                et.answer = DbManager.FillList<TopicAnswer>(ansread);
                if (!ansread.IsClosed) ansread.Close();

                result.Add(et);
            }

            return result;
        }

        // 生成手工题
        private List<ExmTopic> get_manualtopic( int topictype )
        {
            List<ExmTopic> result = new List<ExmTopic>();

            string sql = "SELECT topic_table.*, compute_table.score  FROM topic_table "
                    + " JOIN compute_table on compute_table.topic = topic_table.id "
                    + "WHERE topic_table.id IN ( 	SELECT topic FROM compute_table WHERE model=" + this.id
                    + ") AND topic_table.type =" + topictype;


            DataView view = db.ExecuteDataTableBySql(sql).DefaultView;
            for (int index = 0; index < view.Count; index++ )
            {
                ExmTopic et = new ExmTopic();
                et.id = view[index]["id"].ToInt();
                et.title = view[index]["title"].ToString();
                et.type = topictype;
                et.score = view[index]["score"].ToInt();

                string ans_sql = "SELECT * FROM ANSWER_TABLE WHERE TOPIC = " + et.id;
                IDataReader reader = db.ExecuteReaderBySql(ans_sql);
                et.answer = DbManager.FillList<TopicAnswer>(reader);
                if (!reader.IsClosed) reader.Close();

                result.Add(et);
            }

            return result;
        }

        private List<ExmTopic> switch_topic(int topictype)
        {
            switch (topictype)
            {
                case 1:
                    edm.topic1 = new List<ExmTopic>();
                    return edm.topic1;
                case 2:
                    edm.topic2 = new List<ExmTopic>();
                    return edm.topic2;
                case 3:
                    edm.topic3 = new List<ExmTopic>();
                    return edm.topic3;
                case 4:
                    edm.topic4 = new List<ExmTopic>();
                    return edm.topic4;
                case 5:
                    edm.topic5 = new List<ExmTopic>();
                    return edm.topic5;
                case 6:
                    edm.topic6 = new List<ExmTopic>();
                    return edm.topic6;
                default:
                    throw new Exception(" 发生未知的题型.....");
            }
        }

        // 计算题目总数与总分数
        private void compute_score( int topictype)
        {
            StringBuilder sb = new StringBuilder();

            switch (topictype)
            {
                case 1:
                    edm.topicLen1 = edm.topic1.Count;
                    foreach (ExmTopic item in edm.topic1)
                    {
                        edm.topicscore1 += item.score;
                        sb.Append("INSERT sheet_table(candidate,topic,topictype,score,[full]) VALUES(" +
                            this.candidateID+","+item.id+","+ item.type +",0,"+item.score +");\n");
                    }
                    break;
                case 2:
                    edm.topicLen2 = edm.topic2.Count;
                    foreach (ExmTopic item in edm.topic2)
                    {
                        edm.topicscore2 += item.score;
                        sb.Append("INSERT sheet_table(candidate,topic,topictype,score,[full]) VALUES(" +
                            this.candidateID + "," + item.id + "," + item.type + ",0," + item.score + ");\n");
                    }
                    break;
                case 3:
                    edm.topicLen3 = edm.topic3.Count;
                    foreach (ExmTopic item in edm.topic3)
                    {
                        edm.topicscore3 += item.score;
                        sb.Append("INSERT sheet_table(candidate,topic,topictype,score,[full]) VALUES(" +
                            this.candidateID + "," + item.id + "," + item.type + ",0," + item.score + ");\n");
                    }
                    break;
                case 4:
                    edm.topicLen4 = edm.topic4.Count;
                    foreach (ExmTopic item in edm.topic4)
                    {
                        edm.topicscore4 += item.score;
                        sb.Append("INSERT sheet_table(candidate,topic,topictype,score,[full]) VALUES(" +
                            this.candidateID + "," + item.id + "," + item.type + ",0," + item.score + ");\n");
                    }
                    break;
                case 5:
                    edm.topicLen5 = edm.topic5.Count;
                    foreach (ExmTopic item in edm.topic5)
                    {
                        edm.topicscore5 += item.score;
                        sb.Append("INSERT sheet_table(candidate,topic,topictype,score,[full]) VALUES(" +
                            this.candidateID + "," + item.id + "," + item.type + ",0," + item.score + ");\n");
                    }
                    break;
                case 6:
                    edm.topicLen6 = edm.topic6.Count;
                    foreach (ExmTopic item in edm.topic6)
                    {
                        edm.topicscore6 += item.score;
                        sb.Append("INSERT sheet_table(candidate,topic,topictype,score,[full]) VALUES(" +
                            this.candidateID + "," + item.id + "," + item.type + ",0," + item.score + ");\n");
                    }
                    break;
                default:
                    throw new Exception(" 发生未知的题型.....");
            }

            if (sb.Length > 0)
            {
                DbManager.BeginTransaction();
                DbInstance myDB = DbManager.GetDbInstance();
                try
                {
                    myDB.ExecuteNonQueryBySql(sb.ToString());
                    DbManager.Commit();
                }
                catch (Exception ex)
                {
                    DbManager.Rollback();
                    throw ex;
                }
            }
        }


        // 取得题目信息
        private void get_topic_information( int topictype )
        {
            List<ExmTopic> toplist = switch_topic(topictype);

            if (this.type == 1) // 手工
            {
                toplist.AddRange(get_manualtopic(topictype));
            }
            else   // 随机
            {
                compute_table cp = get_comput_by_type(topictype);
                if (cp == null)
                    return;

                // 随机计算角色题库中的题.
                if (cp.rolecount > 0)
                {
                    toplist.AddRange(get_randomtopic(model_role.id, topictype, cp.rolecount, cp.score));
                }

                // 随机计算公共角色题库中的题.
                if (cp.publiccount > 0)
                {
                    // 取得公共角色.
                    int publicrole = get_public_roleid();
                    toplist.AddRange(get_randomtopic(publicrole, topictype, cp.publiccount, cp.score));
                }
            }

            compute_score(topictype);
        }

        public ExamDataManage Generate()
        {
            // 取得考场的公共信息
            get_examination_information();

            for (int index = 1; index < 7; index++ )
                get_topic_information(index);

            return edm;
        }

        // 取得题目信息
        private void get_topic_information2(int topictype)
        {
            List<ExmTopic> toplist = switch_topic(topictype);

            if (this.type == 1) // 手工
            {
                toplist.AddRange(get_manualtopic(topictype));
            }
            else   // 随机
            {
                compute_table cp = get_comput_by_type(topictype);
                if (cp == null)
                    return;

                // 随机计算角色题库中的题.
                if (cp.rolecount > 0)
                {
                    toplist.AddRange(get_randomtopic(model_role.id, topictype, cp.rolecount, cp.score));
                }

                // 随机计算公共角色题库中的题.
                if (cp.publiccount > 0)
                {
                    // 取得公共角色.
                    int publicrole = get_public_roleid();
                    toplist.AddRange(get_randomtopic(publicrole, topictype, cp.publiccount, cp.score));
                }
            }
            //不生成题目了
           // compute_score(topictype);
        }
        /// <summary>
        /// 获得考过的试题信息
        /// </summary>
        /// <returns></returns>
        public ExamDataManage GenerateView()
        {
            // 取得考场的公共信息
            get_examination_information();

            for (int index = 1; index < 7; index++)
                get_topic_information2(index);

            return edm;
        }
    }
     * */
}
